package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.JComboBox;

import model.CostumerModel;
import model.Employee;
import model.Envelope;
import view.CostumerRelEmpGui;
import view.HRcreateNewEmployeeGui;
import client.CcrmApp;
import controllers.CostumerRelEmpCont.AreaItemListener;
import controllers.CostumerRelEmpCont.RegisterListener;
import controllers.CostumerRelEmpCont.back1Listener;
/**
 * this is controller for human resources of create new employee  
 * @author gili
 *
 */
public class HRcreateNewEmployeeController extends AbstractTransfer {

	private HRcreateNewEmployeeGui HRnewEmpView;
	private HRcontroller prevController;
	//private Employee newEmp;
	private HRcreateNewEmployeeController CurrCon;
	private String AreaSelected = null;

/**
 * 
 * @param HRnewEmpView is Gui of Human Resources instance
 * @param HRcontroller is previous controller instance  
 * 
 */

	public HRcreateNewEmployeeController(HRcreateNewEmployeeGui HRnewEmpView,HRcontroller prevController) {
		// TODO Auto-generated constructor stub
	//	super(costReview,user);
		this.HRnewEmpView = HRnewEmpView;
		this.prevController= prevController;
		CurrCon = this;
		HRnewEmpView.addRegisterActionListener(new RegisterListener());
		HRnewEmpView.addbackBActionListener(new back1Listener());
		HRnewEmpView.addAreaActionListener(new AreaItemListener());
		initArea();
	}
	
	/**
	 * Inner class that handles when Button back Pressed, extends ActiontListener
	 * @author jacob
	 *
	 */
	
	class back1Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			//dispose the current Gui and show the previous Gui 
			HRnewEmpView.dispose();
			prevController.getHRview().setVisible(true);
		}
	
	}
	/**
	 * initialize area combobox with data from server
	 */
	private void initArea() { //send to server to get list of area
		ArrayList<String> area = new ArrayList<String>();
		Envelope ev = new Envelope(area, "get area for employee");
		CcrmApp.clien.setCurrObj(CurrCon);
		sendToServer(ev);
	}
	/**
	 * this method checks validation of input
	 * @return true if data is valid, otherwise return false
	 */
	private boolean checkInput() {
		// TODO Auto-generated method stub
		if(HRnewEmpView.getTextAge() == 0 || HRnewEmpView.getTextAgeTe().equals("") || HRnewEmpView.getTextEmail().equals("") || HRnewEmpView.getTextId().equals("")  
				|| HRnewEmpView.getTextLastName().equals("") || HRnewEmpView.getTextphone().equals("") || HRnewEmpView.getTextUserName().equals("")
				|| AreaSelected == null){	  
			HRnewEmpView.setWarningMessageVisibleTrue("One or more of the fields are empty");
			//HRnewEmpView.ClearText();
		    return false;
		}
		
		else {
			if(HRnewEmpView.getTextAge() < 0) {
				HRnewEmpView.setWarningMessageVisibleTrue("Illigal input of age");
				//HRnewEmpView.ClearText();
			    return false;
			}
		}
		
		return true;
	}	
	

/**
 * this class is a listener for "Register" button
 * it implements ActionListener
 * it draws all data from fields and sends to server to update database
 * @author gili
 *
 */
	class RegisterListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if(checkInput()) {
				/*Employee(String Uid,String lasttname,String firstname,String phone,String email,String address,int Age,int employee_id,String usreName, String upassword,int Position, int status){
    	super(Uid,lasttname,firstname,phone,email,address,Age);*/
				Envelope ev1 = new Envelope(new Employee(HRnewEmpView.getTextId(), HRnewEmpView.getTextLastName(),HRnewEmpView.getTextFieldName().getText(),HRnewEmpView.getTextphone(),HRnewEmpView.getTextEmail(), AreaSelected, 
						 HRnewEmpView.getTextAge(),0, HRnewEmpView.getUserNameTxt().getText(),HRnewEmpView.getPassword().getText(),HRnewEmpView.getPositionC().getSelectedIndex(),0),"Serch employee");
				CcrmApp.clien.setCurrObj(getCurrCon()); //set the client to this controller to return the message
				sendToServer(ev1);
			}
		}
	}
	
	/**
	 * this is a listener for the area combobox
	 * it selects the Area Selected
	 * @author gili
	 *
	 */
	class AreaItemListener implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			JComboBox<String> Combo = getHRnewEmpView().getAreaC();
			AreaSelected = (String)Combo.getSelectedItem();
		}
		
	}

	/**
	 * 
	 *  @param message is an object received from server 
	 *  
	 */
	// @SuppressWarnings("unchecked")
	 @Override
	public void handleDBResult(Object message) {
		 Envelope mes = (Envelope)message;
	   if(mes.getTask().equals("get area for employee")) {
			for(int i = 0; i < ((ArrayList<String>)mes.getObject()).size() ; i++)
				HRnewEmpView.getAreaC().addItem(((ArrayList<String>)mes.getObject()).get(i));
	   }
	   if(mes.getTask().equals("Serch employee")){
		   if(mes.getMess().equals("id exist"))
			   HRnewEmpView.setWarningMessageVisibleTrue("This Id already exists");
		   if(mes.getMess().equals("email exist"))
			   HRnewEmpView.setWarningMessageVisibleTrue("This email already exists");
		   if(mes.getMess().equals("id and email exist"))
			   HRnewEmpView.setWarningMessageVisibleTrue("This Id and email already exists");
		   if(mes.getMess().equals("Insert Employee ok"))
			   HRnewEmpView.setWarningMessageVisibleTrue("Subscription succeeded");
		   if(mes.getMess().equals("User name is already exist"))
			   HRnewEmpView.setWarningMessageVisibleTrue("User name is already exist");
		   if(mes.getMess().equals("id, email and User name are exist"))
			   HRnewEmpView.setWarningMessageVisibleTrue("id, email and User name are exist");
		   
		   HRnewEmpView.ClearText(); 
	   }
	 }
	
	public HRcreateNewEmployeeGui getHRnewEmpView() {
		return HRnewEmpView;
	}

	
	public HRcreateNewEmployeeController getCurrCon() {
		return CurrCon;
	}
	
}
